<?php

namespace app\common\library;

use Exception;
use Intervention\Image\ImageManager;
use think\File;

/**
 * 商品海报生成
 */
class Goodsposter
{
	public $data = [];
	public $baseTpl = "";
	public $image = null;
	public $dirRoot = "";
	public $oss;
	public function __construct($data)
	{
//        $data['tpl']='http://pt-img.sdgjjy.com/uploads/20201026/fa904aba46daaf93319415de4a92355a.png';
        $data['tpl']=ROOT_PATH.'public/assets/img/fa904aba46daaf93319415de4a92355a.png';
		$this->data = $data;
		if(empty($data['goodsName']) || empty($data['tpl']) || empty($data['price']) || empty($data['thumb'])|| empty($data['qrcode_url'])){
			return false;
		}
		$this->dirRoot = ROOT_PATH."public";
		$this->baseTpl = \fast\Utils::toMedia($data['tpl']);
		$this->manager = new ImageManager(array('driver' => 'gd'));
		$this->oss = Aliyun::getOss();
		$this->ossPath ='uploads/goodsposter/' . md5($this->data['tpl'].$this->data['goodsName'].$this->data['price'].$data['orderId']. '_' . $this->data['uid']) . '.jpg';
	}
	/**
	 * 生成海报图
	 */
	public function run()
	{
        $path=ROOT_PATH.'public/'.$this->ossPath;
//		if (!$this->oss->isExists($this->ossPath)) {
//			$dir = dirname($this->ossPath);
//			if (!$this->oss->isExists($dir)) {
//				$this->oss->mkDir($dir);
//			}
        if(!file_exists($this->ossPath)){
			$poster = $this->getPoster();
            $poster->save($this->ossPath,70);
//            header("content-type: image/jpeg");
//            echo $poster->stream('jpg', 85)->getContents();exit;
			//return $this->oss->uploadOs($this->ossPath, $poster->stream('jpg', 70)->getContents());
		}else {
			//return $this->oss->getUrl($this->ossPath);
		}
        return 'http://shop.shoubeikeji.com/'.$this->ossPath;
	}
	/**
	 * 生成海报入口
	 * @return Image|string
	 * @throws Exception
	 */
	public function getPoster() {
		$poster = $this->generateTpl();
		$poster = $this->updateQrcode($poster);
		return $poster;
	}
	/**
	 * 生成海报
	 */
	private function generateTpl()
	{
		// 打开基础模板
		$goodsTpl = $this->manager->make($this->baseTpl)->resize(750, 1200);
		//商品图
        $goodsThumb = $this->manager->make($this->data['thumb'])->resize(120, 120);
        $goodsTpl->insert($goodsThumb, 'top-left', 20, 750);
        $title = $this->data['goodsName'];
        $title = \fast\Utils::mb_chunk_split($title, 20, "\r\n");
        $goodsTpl->text($title, 175, 750, function ($font) {
            /**
             * @var  AbstractFont $font
             */
            $font->file(ROOT_PATH.'public/assets/fonts/SourceHanSansK-Regular.ttf');
            $font->size(28);
            $font->color('#FFF');
            $font->valign('top');
            $font->angle(0);
        });
        $goodsTpl->text('原价 '.$this->data['oPrice'].' 元', 170, 850, function ($font) {
            /**
             * @var  AbstractFont $font
             */
            $font->file(ROOT_PATH.'public/assets/fonts/SourceHanSansK-Regular.ttf');
            $font->size(28);
            $font->color('#FFF');
            $font->valign('top');
            $font->angle(0);
        });
        $goodsTpl->text('拼单价 '.$this->data['price'].' 元', 420, 850, function ($font) {
            /**
             * @var  AbstractFont $font
             */
            $font->file(ROOT_PATH.'public/assets/fonts/SourceHanSansK-Regular.ttf');
            $font->size(30);
            $font->color('#FFF');
            $font->valign('top');
            $font->angle(0);
        });

		return $goodsTpl;
	}
	/**
	 * 添加二维码
	 */
	private function updateQrcode($poster)
	{
		$qrImage = $this->getQrImage($this->data['qrcode_url']);
		$poster->insert($qrImage, 'top-left', 90, 120);
		return $poster;
	}
	/**
	 * @param $qrcode_url
	 * @param int $size
	 * @return \Intervention\Image\Image
	 */
	public function getQrImage($qrcode_url) {
		$qrCode = new \Endroid\QrCode\QrCode($qrcode_url);
		$qrCode->setMargin(30);
		$qrCode->setSize(500);
		$qrCode->setErrorCorrectionLevel('medium');
		$qrImage = $this->manager->make($qrCode->writeString());
		return $qrImage;
	}
}
